<?php

namespace app\mobileapp\controller;

use app\mobileapp\controller\Base;
use think\facade\Cache;
use service\SmsService;
use think\Db;

class File extends Base
{

    /**
     * User: Simony
     * Function:删除相册照片
     */
    public function delImgs()
    {
        $memberId = $this->memberId;
        $absoluteUrl = input('post.absoluteUrl');
        if (empty($absoluteUrl)) $this->api_ajaxReturn(ERROR, '参数错误');
        $fileLogic = model('common/file/FileLogic','logic');
        $fileLogic->fileExists ($absoluteUrl) ?   $fileLogic->deleteFile($absoluteUrl) : $this->api_ajaxReturn(ERROR, '请勿重复删除');
        $this->api_ajaxReturn(SUCCESS, '删除成功');

    }

    /**
     * User: Simony
     * Function:上传图片
     */
    public function uploadImg()
    {
        $memberId = $this->memberId;
        $type = input('post.type', 1);

        //model or logic
        $fileLogic = model('common/file/FileLogic', 'logic');

        $upload_res = $type == 1 ? $fileLogic->uploadImg('disk/content') : $fileLogic->base64Image('disk/content');
        if ($upload_res['code'] != SUCCESS) $this->api_ajaxReturn($upload_res['code'], $upload_res['msg']);


        $this->api_ajaxReturn(SUCCESS, '上传成功', ['absoluteUrl' => $upload_res['result']['absoluteUrl']]);

    }

    /**
     * User: Simony
     * Function:阅后即焚
     */
    public function readBurn()
    {
        $memberId = $this->memberId;
        $member = model('common/member/Member')->getInfoByID($memberId);


        $file_id = input('post.file_id', 0);
        $file = model('common/member/File')->getInfoByID($file_id);
        if (empty($file)) $this->api_ajaxReturn(ERROR, '文件不存在');
        if ($file['type'] != 2) $this->api_ajaxReturn(ERROR, '非阅后即焚文件');


        $photoPayModel = model('common/photo/Pay');


        $imageBlurLogic = model('common/ImageBlur', 'logic');
        $where = [
            'member_id' => $memberId,
            'file_id' => $file_id,
            'type' => 3, //查看类型 1：相册、2：相片、3：阅后即焚
        ];
        $photoPay = $photoPayModel->getInfo($where);
        if($photoPay){ //已阅
            $real_path_old = ROOT_PATH . 'public' . $file['url'];
            $path = $file['url'];
            $path_info = pathinfo($path);

            $new_filename = md5($file['url']) . "." . $path_info['extension'];
            $real_path_new = ROOT_PATH . 'public' . $path_info['dirname'];

            if (is_file($real_path_old) && !is_file($real_path_new . DS . $new_filename)) {
                $imageBlurLogic->gaussian_blur($real_path_old, $real_path_new, $new_filename);
            }
            $file['url'] = $path_info['dirname'] . DS . $new_filename;
            //已阅
            $res = ['absoluteUrl'=>$file['url']];
        }else{

            $data = [
                'type' => 3, //阅后即焚
                'file_id' => $file_id,
                'member_id' => $memberId,
                'view_member_id' => $file['member_id'],
                'expire_time' => time(),
            ];

            $res = $photoPayModel->add($data);
            $res = ['absoluteUrl'=>$file['url']];
        }

        $res ? $this->api_ajaxReturn(SUCCESS, '操作成功',$res) : $this->api_ajaxReturn(ERROR, '操作失败');
    }


    /**
     * User: Simony
     * Function:恢复阅后即焚内容
     */
    public function recoveryReadBurn()
    {
        $memberId = $this->memberId;

        $res = model('common/photo/Pay')->del([
            'type' => 3, //阅后即焚
            'view_member_id' => $memberId,
        ]);

//        $res ? $this->api_ajaxReturn(SUCCESS, '操作成功') : $this->api_ajaxReturn(ERROR, '操作失败');
        $this->api_ajaxReturn(SUCCESS, '操作成功');
    }


    /**
     * User: Simony
     * Function:查看相册照片
     */
    public function viewAblumImg()
    {
        $memberId = $this->memberId;
        $file_id = input('post.file_id/d', 0);
        if ($file_id <= 0) $this->api_ajaxReturn(ERROR, '参数错误');

        $res = $photoPayLogic = model('common/member/memberLogic', 'logic')->viewAblumImg($memberId, $file_id);
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:查看相册照片列表
     */
    public function viewAblumImgList(){
        $memberId = $this->memberId;
        $show_member_id = input('post.show_member_id/d', 0);
        $res = $photoPayLogic = model('common/member/memberLogic', 'logic')->viewAblumImgList($memberId,$show_member_id);
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

}
